package com.sinog.front.controller.biz;

import com.sinog.core.model.UserDTO;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.front.feign.biz.UploadFileTemplateService;
import com.sinog.front.service.user.UserFacade;
import com.sinog.front.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.Map;

/**
 * @desc 数据统计分析 - 报告生成
 * @author sunsf
 * @date 2020年2月26日 7:22
 */
@Slf4j
@RestController
public class UploadFileTemplateController {

    @Resource
    private UploadFileTemplateService uploadFileTemplateService;
    @Resource
    private UserFacade userService;

    /**
     * 模板上传列表
     * @param pageIndex pageIndex
     * @param pageSize pageSize
     * @param request request
     * @return String
     */
    @RequestMapping("/azbj/sjtjfx/scbg/uploadtemplate/getTemplateFileList")
    public String getTemplateFileList(Integer pageIndex,Integer pageSize,HttpServletRequest request) {
        String key = request.getParameter("key");
        return uploadFileTemplateService.getTemplateFileList(pageIndex,pageSize,key);
    }

    /**
     * 数据统计分析--模板管理--保存模板文件--模板
     * @param templateFile templateFile
     * @return Result
     */
    @PostMapping("/azbj/sjtjfx/scbg/uploadtemplate/uploadTemplateFile")
    public Result uploadTemplateFile(@RequestParam("templateFile") MultipartFile templateFile) {
        Map<String,String> path = FileUtil.transfer(templateFile);
        return Result.success(path);
    }

    /**
     * 数据统计分析--模板管理--保存模板文件--模板
     * @param chartFile chartFile
     * @return Result
     */
    @PostMapping("/azbj/sjtjfx/scbg/uploadtemplate/uploadChartFile")
    public Result uploadChartFile(@RequestParam("chartFile") MultipartFile chartFile) {
        Map<String,String> path = FileUtil.transfer(chartFile);
        return Result.success(path);
    }

    /**
     * 全部人员管理--设置帮教小组--保存
     * @param data data
     * @return Result
     */
    @RequestMapping("/azbj/sjtjfx/scbg/uploadtemplate/saveTemplate")
    public Result saveTemplate(@RequestParam(required = false, defaultValue = "-1") String data) {
        UserDTO user = userService.getAuthentication();
        //取数据
        String id = JsonUtil.getValueFromJsonstrByKey(data, "id");
        Date date = new Date();
        if(!StringUtils.equals(id,Constants.NumberStr.NUM_STR_FU_ONE)) {
            data = JsonUtil.setObjectToJsonStr(data, "createuser", user.getDepartid(), "createtime", date);
        }
        data = JsonUtil.setObjectToJsonStr(data, "updateuser", user.getDepartid(), "updatetime", date);
        return uploadFileTemplateService.saveData(data);
    }

    /**
     * 数据统计分析--模板管理--删除模板
     * @param id id
     * @return Result
     */
    @PostMapping("/azbj/sjtjfx/scbg/uploadtemplate/delTemplate")
    public Result delTemplate(@RequestParam("id") String id) {
        Result result = uploadFileTemplateService.delTemplate(id);
        Map<String,String> map = (Map<String,String>)result.getData();
        String path = FileUtil.getFileTemplatePath();
        String chartPath = path + map.get("chartpath");
        String tPath = path + map.get("tpath");
        File chartFile = new File(chartPath);
        if(chartFile.exists()) {
            try {
                chartFile.delete();
            } catch(RuntimeException e) {
                log.error(e.getMessage());
            }
        }
        File tFile = new File(tPath);
        if(tFile.exists()) {
            try {
                tFile.delete();
            } catch(RuntimeException e) {
                log.error(e.getMessage());
            }
        }
        return Result.success("删除成功");
    }
}